package com.hzit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzit.entity.Classes;
import com.hzit.entity.PageResult;
import com.hzit.entity.Student;
import com.hzit.mapper.ClassesMapper;
import com.hzit.mapper.StudentMapper;
import com.hzit.service.StudentService;
import com.hzit.util.MybatisUtil;
import com.hzit.vo.StudentVo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
public class StudentServiceImpl implements StudentService {

	public SqlSessionFactory sqlSessionFactory;
	public StudentServiceImpl() throws IOException {
		sqlSessionFactory =  MybatisUtil.sqlSessionFactory();
	}
	//1. 查询所有学生
	@Override
	public List<StudentVo> findAll() {
		try (SqlSession session = sqlSessionFactory.openSession()) {
			//1.1 获取所有的Mapper对象
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//1.2 查询获取所有的学生列表
			List<Student> students = mapper.findAll();
			List<StudentVo> studentVos = getStudentVos(classesMapper, students);
			//1.3 最后,返回
			return studentVos;
		}
	}

	//2. 进行分页查询
	@Override
	public PageResult<StudentVo> findByPage(Integer page, Integer pageSize) {
		//2.1 开始分页查询
		PageHelper.startPage(page,pageSize);
		//2.2 对所有学生进行查询
		try (SqlSession session = sqlSessionFactory.openSession()) {
			//2.3 获取StudentMapper和ClassesMapper
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//2.4 查询出所有学生列表
			List<Student> students = mapper.findAll();
			//2.5 将学生集合转换为Page对象,注意:只能转换原始与数据库表绑定的实体类,这里是student类,而不能是其它类如:studentVo
			Page<Student> studentPage = (Page<Student>) students;
			//2.6 将结果集数据进行处理,得到新的集合对象
			List<StudentVo> studentVos = getStudentVos(classesMapper, students);
			return new PageResult<>(studentPage.getTotal(),studentVos);
		}

	}



	/**
	 * 3. 根据传入的classesMapper为每个传入的学生集合中的学生指定班级名称
	 * @param classesMapper
	 * @param result
	 * @return
	 */
	private List<StudentVo> getStudentVos(ClassesMapper classesMapper, List<Student> result) {
		List<StudentVo> studentVos = new ArrayList<>();
		//3.1 处理学生对应的班级名称
		for (Student student : result) {
			//3.1.1 定义返回的vo对象
			StudentVo vo = new StudentVo();
			//3.1.2 将student对象的各个属性复制给vo对象的对应的各个属性(两个对象student与vo的属性名一定要一致)
			BeanUtil.copyProperties(student,vo,true);
			//3.1.3 得到学生对应的班级id
			Integer cid = student.getCid();
			//3.1.4 查询出班级对象
			Classes classes = classesMapper.findById(cid);
			//3.1.5 为学生设置班级名称
			vo.setCname(classes.getCname());
			//3.1.6 将学生添加到学生集合中
			studentVos.add(vo);
		}
		return studentVos;
	}
	//4. 条件查询带分页
	@Override
	public PageResult<StudentVo> search(Integer page, Integer pageSize, StudentVo vo) {
		//2.1 开始分页查询
		PageHelper.startPage(page,pageSize);
		//2.2 对所有学生进行查询
		try (SqlSession session = sqlSessionFactory.openSession(true)) {
			//2.3 获取StudentMapper和ClassesMapper
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			ClassesMapper classesMapper = session.getMapper(ClassesMapper.class);
			//2.4 查询出所有学生列表
			List<Student> students = mapper.search(vo);
			//2.5 将学生集合转换为Page对象,注意:只能转换原始与数据库表绑定的实体类,这里是student类,而不能是其它类如:studentVo
			Page<Student> studentPage = (Page<Student>) students;
			//2.6 将结果集数据进行处理,得到新的集合对象
			List<StudentVo> studentVos = getStudentVos(classesMapper, students);
			return new PageResult<>(studentPage.getTotal(),studentVos);
		}

	}

	//3. 添加学生
	@Override
	public void insert(StudentVo studentVo) {
		try (SqlSession session = sqlSessionFactory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.insert(studentVo);
		}
	}

	//4. 修改学生
	@Override
	public void update(StudentVo studentVo) {
		try (SqlSession session = sqlSessionFactory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.update(studentVo);
		}
	}

	//5. 删除学生
	@Override
	public void deleteById(String sid) {
		try (SqlSession session = sqlSessionFactory.openSession(true)) {
			StudentMapper mapper = session.getMapper(StudentMapper.class);
			mapper.deleteById(sid);
		}
	}
}
